<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Db;
use think\facade\Event;

/**
 * @mixin \think\Model
 */
class Version extends Base
{
    protected $name = 'version';

    public static function _save($param): array
    {
        try {
            if (isset($param['id']) && $param['id']) {
                $version = self::find($param['id']);
                $version->save($param);
            } else {
                $version = parent::create($param);
            }

            self::where('type', $version['type'])->save(['is_new'=>0]);
            $new = self::where('type', $version['type'])->where('is_del', 0)->order('id', 'desc')->find();
            $new->is_new = 1;
            $new->save();

            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
